package emulator;

import java.util.HashSet;

import keyboard.Keyboard;
import keyboard.Position;

public class ResultCostCalculatorFittsLaw extends ResultCostCalculator {
	public ResultCostCalculatorFittsLaw(String strategy, int dictSize) {
		super(strategy, dictSize);
		this.costType = "fittsLaw";
	}
	
	public float CalnCost(Keyboard keyboard, Result result) {
		String word = result.GetWord();
		float cost = 0f;
		
		Position lastPosition = keyboard.GetLetterPosition('g');
		for (int i = 0; i < word.length(); i++) {
			keyboard.GroupingStaticKeyboard(result.GetExpression(i));
			HashSet<Character> keysGroup = keyboard.GetKeysGroupOfLetter(word.charAt(i));
			Position centre = keyboard.GetGroupCentrePosition(keysGroup);
			Position size = keyboard.GetGroupSize(keysGroup);
			Position position = Position.getPressPosition(lastPosition, centre, size);
			cost += Position.EuclidDist(lastPosition, position);
			lastPosition = position;
		}
		
		int priority = result.GetPriority();
		float scanCost = 0f;
		int pos = (priority - 1) % 4 + 1;
		int page = (priority - 1) / 4 + 1;
		if (priority <= 4) {
			Position confirmCentre = keyboard.GetSelectorCentrePosition(pos);
			Position confirmSize = keyboard.GetSelectorSize();
			Position confirmPosition = Position.getPressPosition(lastPosition, confirmCentre, confirmSize);
			scanCost = Position.EuclidDist(lastPosition, confirmPosition);
		} else {
			Position nextCentre = keyboard.GetNextCentrePosition();
			Position nextSize = keyboard.GetSelectorSize();
			Position nextPosition = Position.getPressPosition(lastPosition, nextCentre, nextSize);
			Position confirmCentre = keyboard.GetSelectorCentrePosition(pos);
			Position confirmSize = keyboard.GetSelectorSize();
			Position confirmPosition = Position.getPressPosition(nextPosition, confirmCentre, confirmSize);
			scanCost = Position.EuclidDist(lastPosition, nextPosition) + Position.EuclidDist(nextPosition, confirmPosition);
			scanCost += (page - 1) * Keyboard.layout[0].length;
		}
		cost += scanCost;
		
		return cost;
	}
}
